import org.springframework.validation.FieldError;

import job.tracker.TestSaveBy;
import job.tracker.domain.*;
import job.tracker.service.JobService;

class BootStrap {

	User testUser
	JobService jobService
	def grailsApplication

	def init = { servletContext ->
		
		//new TestSaveBy(bySel : By.cssSelector("apple")).save(flush: true)
		
//		println TestSaveBy.list() 
		
		testUser = new User(email: 'maheesi@yahoo.com', admin: true).save(flush: true, failOnError: true)

		Class domainName = JobSite


		//createSiteUnlessExists(jobSelector: 'item', name: 'ASSL', description: 'ASSL', pageUrl: 'http://localhost/dummysite/', dataElement: 'div', domainName: domainName)
		createSiteUnlessExists(jobSelector: 'h2', dataElement: '.about_evertue_content > div', name: 'evertrue', pageUrl: 'http://www.evertrue.com/careers', description: 'go jamaica IT', domainName: domainName)
		createSiteUnlessExists(jobSelector: 'li', dataElement: 'ul.fs_v.fs_13', name: 'shareaholic', pageUrl: 'http://www.shareaholic.com/careers/', description: 'go jamaica IT', domainName: domainName)
		//FIXME: no open positions 
		//createSiteUnlessExists(jobSelector: '', dataElement: '.jobList', name: 'azavea', pageUrl: 'http://www.azavea.com/about-us/join-us/current-job-openings/', description: 'go jamaica IT', domainName: domainName)
		createSiteUnlessExists(jobSelector: 'li', dataElement: '.g1 > ul', name: 'grabcad', pageUrl: 'http://blog.grabcad.com/jobs/', description: 'go jamaica IT', domainName: domainName)
		createSiteUnlessExists(jobSelector: 'li', dataElement: '#right_content > ul', name: 'practicallygreen', pageUrl: 'http://practicallygreen.com/jobs/', description: 'go jamaica IT', domainName: domainName)
		createSiteUnlessExists(jobSelector: 'li', dataElement: 'div.col12.right.h1-align > ul', name: 'silverstripe', pageUrl: 'http://www.silverstripe.com/about-us/careers/', description: 'go jamaica IT', domainName: domainName)
		createSiteUnlessExists(jobSelector: 'em', dataElement: 'table[width=449] | tr:eq(1) | table:eq(1)', name: 'orcjamaica', pageUrl: 'http://www.orcjamaica.com/', description: 'go jamaica IT', domainName: domainName)
		createSiteUnlessExists(jobSelector: 'a', dataElement: '#lvProducts_position', name: 'trinidadvacancies.com it', pageUrl: 'http://www.trinidadvacancies.com/categories.aspx?catid=33', description: 'go jamaica IT', domainName: domainName)
		createSiteUnlessExists(jobSelector: 'b', name: 'go jamaica IT', description: 'go jamaica IT', pageUrl: 'http://go-jamaica.com/jobsmart/query.php?jobtitle=0&category=3', dataElement: '.careerright', domainName: domainName)
		createSiteUnlessExists(jobSelector: 'tr', name: 'ASSL', description: 'ASSL', pageUrl: 'http://www.assl.com/Employment.htm', dataElement: '.smltextequip > table', domainName: domainName)
		createSiteUnlessExists(jobSelector: 'tr', name: 'diicel caribbean IT', description: 'Digicel caribbean IT', pageUrl: 'http://www.candidatemanager.net/cm/Micro/SearchResults.aspx?&mid=BCXUGT&SCH=YY&a=kArYLwQxQHg%3d&VJ=0&key=&CC=&RC=&SC=&S=&JC=&CJF=1510&CL=0&CJCS=', dataElement: '#tblSearchResults', domainName: domainName)
		createSiteUnlessExists(jobSelector: 'li', dataElement: '.span4.offset1.positions > ul', name: 'ginger.io', pageUrl: 'http://www.ginger.io/Main/WereHiring', description: 'go jamaica IT', domainName: domainName)
		createSiteUnlessExists(jobSelector: 'a', dataElement: '#jobs', name: 'recruitment.gov.ky', pageUrl: 'http://www.gov.ky/portal/page?_pageid=1142,1941597&_dad=portal&_schema=PORTAL&p_lowrow=1&p_highrow=10', description: 'go jamaica IT', domainName: domainName)
		createSiteUnlessExists(jobSelector: 'a', dataElement: '#jobs', name: 'recruitment.gov.ky2', pageUrl: 'http://www.gov.ky/portal/page?_pageid=1142,1941597&_dad=portal&_schema=PORTAL&p_lowrow=11&p_highrow=20', description: 'go jamaica IT', domainName: domainName)
		createSiteUnlessExists(jobSelector: 'a', dataElement: '#jobs', name: 'recruitment.gov.ky3', pageUrl: 'http://www.gov.ky/portal/page?_pageid=1142,1941597&_dad=portal&_schema=PORTAL&p_lowrow=21&p_highrow=30', description: 'go jamaica IT', domainName: domainName)
		createSiteUnlessExists(jobSelector: 'span[class=largeText]', dataElement: '#dnn_ctr534_Repository_lstObjects', name: 'caymanlandinfo.ky', pageUrl: 'http://www.caymanlandinfo.ky/AboutUs/Jobs/tabid/81/Default.aspx', description: 'go jamaica IT', domainName: domainName)
		createSiteUnlessExists(jobSelector: '> a', dataElement: '.main_content', name: 'pod', pageUrl: 'http://podconsulting.com/careers.html', description: 'go jamaica IT', domainName: domainName)
		createSiteUnlessExists(jobSelector: 'b', dataElement: '#table29 | tr:eq(1) | table', name: 'www.ansamcal.com', pageUrl: 'http://www.ansamcal.com/eng/1careers.asp', description: 'go jamaica IT', domainName: domainName)
		//post		
		createSiteUnlessExists(jobSelector: 'div.articletitle', dataElement: '#ctl00_MainContent_ASPxRoundPanel1_JobListing1_gvwCareers', name: 'serha.gov.jm', pageUrl: 'http://www.serha.gov.jm/(A(uiacWhwMzQEkAAAAOWIwODJiODQtZTNmZi00ZTBhLWFlOTAtN2Q2OTM3MDY1OWIws5Cqfme5wJdUBVh7x9e7sEib28w1))/BrowseVacancies.aspx', description: 'go jamaica IT', domainName: domainName)
		createSiteUnlessExists(jobSelector: 'b', dataElement: '#maincontent-block > table', name: 'serha.gov.jm', pageUrl: 'http://trinidadjob.com/index.php', description: 'go jamaica IT', domainName: domainName, postDataParams : [ option:'com_recruitmentmanager', task:'search', Itemid:'1', sector:'37', area:'', searchfor:'', search:'Search'])
		
		

		//prolly not worth parsing
		//createSiteUnlessExists(dataElement: '#resumator-wrapper', name: 'yesware', pageUrl: 'http://www.yesware.com/jobs/', description: 'go jamaica IT', domainName: domainName)
		//createSiteUnlessExists(name: 'diicel caribbean IT', description: 'Digicel caribbean IT', pageUrl: 'http://www.shoestringbarbados.com/travel_jobs.htm', dataElement: 'td[width=569]', domainName: domainName)
	}

	def destroy = {
	}

	def createSiteUnlessExists(Map opts) {
		if (!opts.domainName.findByPageUrl(opts.pageUrl)) {
			opts.approvalStatus = JobSiteApprovalStatusEnum.APPROVED
			JobSite js = opts.domainName.newInstance(opts)//.save(flush: true, failOnError: true)
			jobService.saveJobSite(testUser, js);
		}
	}

	def setUpRenderErrorForControllers() {
		//		grailsApplication.controllerClasses.each { controllerArtefact ->
		//
		////			controllerArtefact.metaClass.renderError { String message ->
		////				render(contentType: "text/json", status: 500) { errorMessage = message }
		////			}
		//			def controllerClass = controllerArtefact.getClazz()
		//			controllerClass.metaClass.renderError { String message ->
		//				render(contentType: "text/json", status: 500) { errorMessage = message }
		//			}
		//			controllerClass.metaClass.renderError { List<FieldError> field_errors ->
		//				render(contentType: "text/json", status: 500) {
		//					fieldErrors = array {
		//						field_errors.each {
		//							err(defaultMessage: it.defaultMessage, field: it.field, rejectedValue: it.rejectedValue)
		//						}
		//					}
		//				}
		//			}
		//
		//            println "$controllerArtefact, $controllerClass"
		//		}

	}
}
